package com.mifengs.commonbase.base;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.alibaba.sdk.android.httpdns.HttpDnsService;
import com.mifengs.commonbase.BuildConfig;
import com.mifengs.commonbase.Constant;
import com.mifengs.commonbase.cache.DiskLruCacheHelper;
import com.mifengs.commonbase.di.component.AppComponent;
import com.mifengs.commonbase.di.component.DaggerAppComponent;
import com.mifengs.commonbase.di.module.AppModule;
import com.squareup.leakcanary.LeakCanary;

public abstract class BaseApplication extends Application {

    public static BaseApplication instance;
    private static AppComponent mAppComponent;
    private DiskLruCacheHelper mCacheHelper;
    private HttpDnsService httpdns;
    public HttpDnsService gethttpdns() {
        return httpdns;
    }

    public static BaseApplication getInstance() {
        return instance;
    }


    @Override
    public void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initInjector();

//        if (BuildConfig.DEBUG) {           // 这两行必须写在init之前，否则这些配置在init过程中将无效
//            ARouter.openLog();     // 打印日志
//            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
//        }
//        ARouter.init(this);
//
//        httpdns = HttpDns.getService(this,"115163","598c01f38e264a7db71493d5b6948445");
//        try {
//            URL url = new URL(Constant.BaseUrl);
//            ArrayList<String> hostList = new ArrayList<>();
//            hostList.add(url.getHost());
//            httpdns.setHTTPSRequestEnabled(true); //设置https
//            httpdns.setPreResolveHosts(hostList); //设置预解析域名列表
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }

        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
//        UMShareAPI.get(this);
//        PlatformConfig.setWeixin("wx960f4cfc839d5921","833992b1a0f085cad677149a4e0fa9d9");
//        PlatformConfig.setQQZone("1106424327","R7pQGd4i8AVfhoLR");
        initCache();
    }

    private void initCache() {
        try {
            mCacheHelper = new DiskLruCacheHelper(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public DiskLruCacheHelper getCacheHelper() {
        return mCacheHelper;
    }


    /**
     * 初始化注射器
     */
    private void initInjector() {
        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }

    public static AppComponent getAppComponent() {
        return mAppComponent;
    }

}
